﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;
using System.Threading;
using System.Globalization;
using System.Resources;

using EHTWebsite.Code;

namespace EHTWebsiteProject.Controls
{
    public partial class CustomerTopControl : System.Web.UI.UserControl
    {
        public Boolean IsVN = true;
        private String languageCode = "vi-VN";
        private ResourceManager rm = new ResourceManager("Resources.SiteMap", System.Reflection.Assembly.Load("App_GlobalResources"));

        private Int32 pageIndex = 1;
        private Int32 pageSize = 100000;
        private Int32 totalRecord = 0;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                CreatePanel();
            }
        }

        private void CreatePanel()
        {
            KhachHang theKhachHang = new KhachHang();
            theKhachHang.KhachHangID = -1;
            theKhachHang.DiaChiOther = "";
            theKhachHang.DiaChiVN = "";
            theKhachHang.DiDong = "";
            theKhachHang.DienThoai = "";
            theKhachHang.Email = "";
            theKhachHang.GhiChuOther = "";
            theKhachHang.GhichuVN = "";
            theKhachHang.TenKhachHangOther = "";
            theKhachHang.TenKhachHangVN = "";

            DataSet dsKhachHangs = KhachHangProvider.GetKhachHangsByFillter(theKhachHang, pageIndex, pageSize, out totalRecord);
            StringBuilder sb = new StringBuilder();
            Int32 amoutOfKhachHang = dsKhachHangs.Tables[0].Rows.Count;
            Int32 pageIndex1 = 1;
            Int32 itemPerPage = 6;
            Int32 currentItem = 0;

            for (Int32 i = 0; i < amoutOfKhachHang; ++i)
            {
                currentItem += 1;

                if (currentItem == 1)
                {
                    sb.Append("<div class='panel'>");
                    sb.Append("<div class='panel-wrapper'>");

                    sb.Append("<h2 class='title' style='visibility:hidden; margin-top:-20px;'>");
                    sb.Append("Page " + pageIndex1.ToString());
                    sb.Append("</h2>");
                    sb.Append("<div class='anh-panel'>");
                }

                sb.Append("<div class='images-panel img'>");
                sb.Append("<a href='/khach-hang-chi-tiet/" + dsKhachHangs.Tables[0].Rows[i]["KhachHangID"].ToString() + "/" + FormatStringForUrl(dsKhachHangs.Tables[0].Rows[i]["TenKhachHangVN"].ToString()) + ".aspx'>");
                sb.Append("<img src='/ImageDatas/Customers/" + dsKhachHangs.Tables[0].Rows[i]["HinhAnhUrl"].ToString() + "' width='250px' height='160px' border='0' />");
                sb.Append("</a>");
                //sb.Append("<br />");
                sb.Append("<a href='/khach-hang-chi-tiet/" + dsKhachHangs.Tables[0].Rows[i]["KhachHangID"].ToString() + "/" + FormatStringForUrl(dsKhachHangs.Tables[0].Rows[i]["TenKhachHangVN"].ToString()) + ".aspx'>");
                sb.Append(CatString(dsKhachHangs.Tables[0].Rows[i]["TenKhachHangVN"].ToString()));
                sb.Append("</a>");
                sb.Append("</h3>");
                sb.Append("</div>");


                if (currentItem == itemPerPage || (i == amoutOfKhachHang - 1 && currentItem < itemPerPage))
                {
                    //sb.Append("<div style='clear:both;'></div");
                    sb.Append("</div>");

                    sb.Append("</div>");
                    sb.Append("</div>");
                    currentItem = 0;
                    pageIndex1 += 1;
                }
            }
            this.ltrKhachHangs.Text = sb.ToString();
        }

        private String FormatStringForUrl(String input)
        {
           return HelpMethod.FormatStringForUrl(input);
        }

        private String CatString(String input)
        {
            return HelpMethod.CatString(input, 25);
        }
    }
}